<template>
  <div id="app">
    <!-- 导航栏组件 -->
    <header class="app-header">
      <nav class="main-nav">
        <div class="nav-container">
          <!-- 品牌标识 -->
          <div class="brand">
            <router-link to="/" class="brand-link">ShopAdmin</router-link>
          </div>
          
          <!-- 导航链接 - 桌面端 -->
          <ul class="nav-links">
            <li v-for="(item, index) in navLinks" :key="index">
              <router-link 
                :to="item.path" 
                :class="['nav-link', isActive(item.path) ? 'active' : '']"
                @click="handleNavClick"
              >
                {{ item.name }}
              </router-link>
            </li>
          </ul>
          
          <!-- 移动端菜单按钮 -->
          <button 
            class="menu-toggle" 
            @click="isMenuOpen = !isMenuOpen"
            aria-label="切换菜单"
          >
            <i class="fas" :class="isMenuOpen ? 'fa-times' : 'fa-bars'"></i>
          </button>
        </div>
        
        <!-- 移动端导航菜单 -->
        <div class="mobile-menu" :class="{ 'mobile-menu-open': isMenuOpen }">
          <ul>
            <li v-for="(item, index) in navLinks" :key="index">
              <router-link 
                :to="item.path" 
                :class="['mobile-nav-link', isActive(item.path) ? 'active' : '']"
                @click="isMenuOpen = false"
              >
                {{ item.name }}
              </router-link>
            </li>
          </ul>
        </div>
      </nav>
    </header>
    
    <!-- 主内容区 -->
    <main class="main-content">
      <!-- 路由加载状态 -->
      <div v-if="isLoading" class="loading-indicator">
        <div class="spinner"></div>
        <p>加载中...</p>
      </div>
      
      <!-- 路由视图容器 -->
      <router-view v-else />
      
      <!-- 错误提示 -->
      <div v-if="hasError" class="error-notification">
        <p>{{ errorMessage }}</p>
        <button @click="clearError">关闭</button>
      </div>
    </main>
    
    <!-- 页脚 -->
    <footer class="app-footer">
      <p>&copy; {{ new Date().getFullYear() }} ShopAdmin 管理系统</p>
    </footer>
  </div>
</template>

<script setup>
// 移除了未使用的 computed 导入
import { ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';

// 导航链接数据
const navLinks = [
  { path: '/', name: '首页' },
  { path: '/products', name: '商品管理' },
  // 可在此处轻松添加更多导航项
  // { path: '/orders', name: '订单管理' },
  // { path: '/users', name: '用户管理' }
];

// 状态管理
const isMenuOpen = ref(false);
const isLoading = ref(false);
const hasError = ref(false);
const errorMessage = ref('');

// 路由相关
const route = useRoute();
const router = useRouter();

// 判断当前路由是否激活
const isActive = (path) => {
  return route.path === path;
};

// 处理导航点击
const handleNavClick = () => {
  // 在桌面端点击导航时也可以添加额外逻辑
};

// 清除错误提示
const clearError = () => {
  hasError.value = false;
  errorMessage.value = '';
};

// 监听路由变化，控制加载状态
watch(
  () => route.path,
  (newPath, oldPath) => {
    if (newPath !== oldPath) {
      isLoading.value = true;
      
      // 模拟加载延迟（实际项目中可根据API请求状态调整）
      const timer = setTimeout(() => {
        isLoading.value = false;
        clearTimeout(timer);
      }, 300);
    }
  }
);

// 路由守卫 - 处理全局错误
router.beforeEach((to, from, next) => {
  clearError();
  next();
});

// 捕获路由错误
router.onError((error) => {
  hasError.value = true;
  errorMessage.value = `路由加载失败: ${error.message}`;
  isLoading.value = false;
});
</script>

<style scoped>
/* 基础样式 */
#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

/* 头部导航 */
.app-header {
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.main-nav {
  width: 100%;
}

.nav-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  max-width: 1200px;
  margin: 0 auto;
}

/* 品牌标识 */
.brand {
  padding: 15px 0;
}

.brand-link {
  font-size: 1.5rem;
  font-weight: bold;
  color: #42b983;
  text-decoration: none;
  transition: opacity 0.3s ease;
}

.brand-link:hover {
  opacity: 0.8;
}

/* 导航链接 - 桌面端 */
.nav-links {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
}

.nav-link {
  display: block;
  padding: 20px 15px;
  color: #2c3e50;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
}

.nav-link:hover:not(.active) {
  color: #42b983;
}

.nav-link.active {
  color: #42b983;
}

.nav-link.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background-color: #42b983;
}

/* 移动端菜单按钮 */
.menu-toggle {
  display: none;
  background: none;
  border: none;
  font-size: 1.5rem;
  color: #2c3e50;
  cursor: pointer;
  padding: 10px;
}

/* 移动端导航菜单 */
.mobile-menu {
  display: none;
  background-color: #ffffff;
  border-top: 1px solid #f1f1f1;
}

.mobile-menu ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.mobile-nav-link {
  display: block;
  padding: 15px 20px;
  color: #2c3e50;
  text-decoration: none;
  border-bottom: 1px solid #f1f1f1;
  transition: background-color 0.3s ease;
}

.mobile-nav-link:hover:not(.active) {
  background-color: #f8f9fa;
}

.mobile-nav-link.active {
  color: #42b983;
  background-color: #f0f9f5;
}

.mobile-menu-open {
  display: block;
}

/* 主内容区 */
.main-content {
  flex: 1;
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
  box-sizing: border-box;
}

/* 加载指示器 */
.loading-indicator {
  padding: 40px 0;
  text-align: center;
}

.spinner {
  width: 40px;
  height: 40px;
  margin: 0 auto 15px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #42b983;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 错误提示 */
.error-notification {
  background-color: #fff0f0;
  border: 1px solid #ffcccc;
  border-radius: 4px;
  padding: 15px;
  margin: 15px 0;
  color: #cc0000;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.error-notification button {
  background-color: #ff4444;
  color: white;
  border: none;
  border-radius: 3px;
  padding: 5px 10px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.error-notification button:hover {
  background-color: #cc0000;
}

/* 页脚 */
.app-footer {
  padding: 20px;
  background-color: #f8f9fa;
  color: #777;
  font-size: 0.9rem;
  margin-top: auto;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-links {
    display: none;
  }
  
  .menu-toggle {
    display: block;
  }
  
  .main-content {
    padding: 15px;
  }
  
  .brand-link {
    font-size: 1.2rem;
  }
}
</style>
